/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.agile.main;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Insets;
import java.awt.Polygon;
import java.awt.Shape;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.Random;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JPanel;

/**
 *
 * @author Rko
 */
public class Canvas extends JPanel {

    // Named-constants for dimensions
    public static final int CANVAS_WIDTH = 640;
    public static final int CANVAS_HEIGHT = 480;
    public static final String TITLE = "Affine Transform Demo";

    // Define an arrow shape using a polygon centered at (0, 0)
    int[] polygonXs = {-20, 0, +20, 0};
    int[] polygonYs = {20, 10, 20, -20};
    Shape shape = new Polygon(polygonXs, polygonYs, polygonXs.length);
    double x = 50.0, y = 50.0;  // (x, y) position of this Shape

    /**
     * Constructor to set up the GUI components
     */
    public Canvas() {
        setPreferredSize(new Dimension(CANVAS_WIDTH, CANVAS_HEIGHT));
    }

    /**
     * Custom painting codes on this JPanel
     */
    @Override
    public void paintComponent(Graphics g) {

            super.paintComponent(g);    // paint background
            setBackground(Color.WHITE);
            Graphics2D g2d = (Graphics2D) g;
            g2d.setColor(Color.blue);

            File file = new File("E:\\agile-pipei\\enabtmr.GBL");
            String line;
            int oldx = 0, oldy = 0;

            BufferedImage bi = new BufferedImage(1000, 1000, BufferedImage.TYPE_INT_ARGB);

            try (BufferedReader input = new BufferedReader(new FileReader(file))) {
                line = input.readLine();
                while (line != null) {
                    if (line.charAt(0) == 'X') {
                        String strx = line.split("X")[1];
                        System.out.println(strx);
                        String[] x = strx.split("Y");
                        System.out.println(x[0]);//X

                        int intx = Integer.parseInt(x[0]);

                        strx = x[1];
                        String[] y = strx.split("D");
                        System.out.println(y[0]);//Y

                        int inty = Integer.parseInt(y[0]);

                        strx = y[1];
                        String[] d = strx.split("\\*");
                        System.out.println(d[0]);//D
                        if (d[0].equals("01")) {//line
                            g2d.drawLine(oldx, oldy, intx / 50, inty / 50);
                            oldx = intx / 50;
                            oldy = inty / 50;
                        }
                        if (d[0].equals("02")) {//point
                            oldx = intx / 50;
                            oldy = inty / 50;
                        }

                    }
                    line = input.readLine();
                }
            } catch (IOException e) {

            }
            //ImageIO.write(bi, "JPEG", new File("e:\\yourImageName.JPG"));

            /*Dimension size = getSize();
             Insets insets = getInsets();
            
             int w = size.width - insets.left - insets.right;
             int h = size.height - insets.top - insets.bottom;
            
             Random r = new Random();
            
             for (int i = 0; i < 1000; i++) {
            
             int x = Math.abs(r.nextInt()) % w;
             int y = Math.abs(r.nextInt()) % h;
             g2d.drawLine(x, y, x, y);
             }*/
        
    }

}
